﻿using System;
using System.Windows.Forms;

namespace GtalkBot
{
    public partial class Login : Form
    {
        Gbot Gbot;
        public INIClass config = new INIClass(Application.StartupPath + "/config.ini");
        
        public Login()
        {
            InitializeComponent();
            if (WinIO32.InitializeWinIo()==false) {
                MessageBox.Show("winio驱动加载失败");
            }
            

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) //注册
        {
            if (e.Button == MouseButtons.Left)
            {
                System.Diagnostics.Process.Start("https://accounts.google.com/newaccount");
                //这里是按了左键则执行的时间
            }
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)  //忘记密码
        {
            if (e.Button == MouseButtons.Left)
            {
                System.Diagnostics.Process.Start("https://www.google.com/accounts/recovery");
                //这里是按了左键则执行的时间
            }
        }

        private void btnSave_Click(object sender, EventArgs e)  //登录按钮
        {
            backgroundWorker2.CancelAsync();
            panel2.Visible = true;
            if (UserName.Text.Trim() == "")
            {
                panel2.Visible = true;
                lblStatus.Text = "请输入账号";
                UserName.Focus();
                return;
            }

            if (Password.Text.Trim() == "")
            {
                panel2.Visible = true;
                lblStatus.Text = "请输入密码";
                Password.Focus();
                return;
            }



            if (Remember.Checked == true)
            {
                config.IniWriteValue("LoginInfo", "username", UserName.Text.Trim());
                config.IniWriteValue("LoginInfo", "password", Password.Text.Trim());
                config.IniWriteValue("LoginInfo", "Remember", "true");
            }
            else
            {
                config.IniWriteValue("LoginInfo", "username", "");
                config.IniWriteValue("LoginInfo", "password", "");
                config.IniWriteValue("LoginInfo", "Remember", "false");
            }
            config.IniWriteValue("LoginInfo", "AutoLogin", AutoLogin.Checked ? "true" : "false");

            btnSave.Enabled = false;

            lblStatus.Text = "正在登录...";

            Gbot = new Gbot(UserName.Text.Trim(), Password.Text.Trim());

            backgroundWorker1.RunWorkerAsync();



        }


        private void Login_Load(object sender, EventArgs e)  //页面载入
        {
            Control.CheckForIllegalCrossThreadCalls = false;



            UserName.Text = config.IniReadValue("LoginInfo", "username");
            Password.Text = config.IniReadValue("LoginInfo", "password");
            if (config.IniReadValue("LoginInfo", "Remember") == "true")
            {
                Remember.Checked = true;
            }
            if (config.IniReadValue("LoginInfo", "AutoLogin") == "true")
            {
                AutoLogin.Checked = true;
                backgroundWorker2.RunWorkerAsync();
            }


        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            while (Gbot.Status == "正在登录")
            {
                System.Threading.Thread.Sleep(500);
            }
            backgroundWorker1.ReportProgress(1);
            backgroundWorker1.Dispose();
        }

        private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 1)
            {
                if (Gbot.Status == "登录成功")
                {
                    lblStatus.Text = "登录成功";
                }
                else
                {
                    btnSave.Enabled = true;
                    lblStatus.Text = Gbot.Status;
                }
            }
        }

        private void backgroundWorker2_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            int ss = 6;
            while (ss > 0)
            {
                if (backgroundWorker2.CancellationPending)
                {
                    backgroundWorker2.Dispose();
                    return;
                }
                ss--;
                backgroundWorker2.ReportProgress(ss);
                System.Threading.Thread.Sleep(1000);
            }
            backgroundWorker2.Dispose();
        }

        private void backgroundWorker2_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            btnSave.Text = "登录（" + e.ProgressPercentage + "）";
            if (e.ProgressPercentage == 0)
            {
                btnSave_Click("", null);

            }
        }

        private void AutoLogin_Click(object sender, EventArgs e)
        {
            backgroundWorker2.CancelAsync();
        }

        private void Login_SizeChanged(object sender, EventArgs e)
        {

            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                notifyIcon1.Visible = true;

            }







        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void cfg_Click(object sender, EventArgs e)
        {
            cfg cfg = new cfg();
            cfg.ShowDialog();


        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 AboutBox1 = new AboutBox1();
            AboutBox1.ShowDialog();
        }

        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            WinIO32.ShutdownWinIo();
        }










    }
}
